//登录 登出 获取信息
import { login, logout, getInfo } from '@/api/user'
//获取token 设置token 销毁token
import { getToken, setToken, removeToken } from '@/utils/auth'
//重置路由
import { resetRouter } from '@/router'

//user模块信息的储存
const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    introduction:'',
    age:''
  }
}

const state = getDefaultState()

const mutations = {
  //重置state
  RESET_STATE: (state) => {
    //后面对象属性添加给前面state
    Object.assign(state, getDefaultState())
  },
  //赋值token  state,payload=>payload.token
  SET_TOKEN: (state, token) => {
    state.token = token
  },
/*  SET_TOKEN: (state, payload) => {
    state.token = payload.token
  },*/
  //赋值name
  SET_NAME: (state, name) => {
    state.name = name
  },
  //赋值头像
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  //赋值描述信息 success了
  introduction: (state, introduction) => {
    state.introduction = introduction
  },
  //赋值age
  age: (state, age) => {
    state.age = age
  }
}

const actions = {
  //  用户登录  ！！！！！ 原本写法context,payload =>context.commit,payload.userInfo
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        //后台返回的结果在这里  表示登录成功
        console.log("登录成功")
        console.log(response)
        const { data } = response
        //将对应的token放在vuex管理
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

   /*
    context的参数说明:
    state,       等同于store.$state，若在模块中则为局部状态
		rootState,   等同于store.$state,只存在模块中
		commit,      等同于store.$commit
		dispatch,    等同于store.$dispatch
		getters      等同于store.$getters
  */

  //获取用户信息 （谁第一次调用了这个东西?） 一般都这样写:context.commit  context.state
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        console.log("获取用户信息成功:")
        console.log(response)
        const { data } = response
        // console.log(data)
        if (!data) {
          return reject('Verification failed, please Login again.')
        }
        //添加介绍
        const { name, avatar,introduction,age} = data

        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        //介绍
        commit('introduction',introduction)
        commit('age',age)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 退出登录
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 移除token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}
console.log("用户信息:")
console.log(state)


export default {
  namespaced: true,
  state,
  mutations,
  actions
}

